package com.zzyweb.rentalcarsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzyweb.rentalcarsys.entity.UserAccount;
import com.zzyweb.rentalcarsys.entity.UserInfo;
import com.zzyweb.rentalcarsys.entity.relationship.UserAndRole;
import com.zzyweb.rentalcarsys.exception.BusinessException;
import com.zzyweb.rentalcarsys.exception.ErrorCode;
import com.zzyweb.rentalcarsys.mapper.UserAccountMapper;
import com.zzyweb.rentalcarsys.mapper.UserInfoMapper;
import com.zzyweb.rentalcarsys.mapper.UserRoleMapper;
import com.zzyweb.rentalcarsys.service.UserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

/**
 * @author zhiyi
 * @date 9/29/25
 **/
@Service
public class UserAccountServiceImpl implements UserAccountService {
    @Autowired
    UserAccountMapper userAccountMapper;
    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    UserRoleMapper userRoleMapper;
    @Override
    public void register(UserAccount userAccount) {
        //进行业务逻辑校验
        QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<>();
        //检查用户名
        queryWrapper.eq(StringUtils.hasText(userAccount.getUsername()),"username", userAccount.getUsername());
        UserAccount checkUsername = userAccountMapper.selectOne(queryWrapper, true);
        if (checkUsername != null) throw new BusinessException(ErrorCode.EXISTED_USERNAME);
        queryWrapper.clear();

        //检查邮箱
        queryWrapper.eq(StringUtils.hasText(userAccount.getEmail()),"email", userAccount.getEmail());
        UserAccount checkEmail = userAccountMapper.selectOne(queryWrapper, true);
        if (checkEmail != null) throw new BusinessException(ErrorCode.EXISTED_EMAIL);
        queryWrapper.clear();

        //检查电话号码
        queryWrapper.eq(StringUtils.hasText(userAccount.getPhoneNumber()),"phone_number", userAccount.getPhoneNumber());
        UserAccount checkPhoneNum = userAccountMapper.selectOne(queryWrapper, true);
        if (checkPhoneNum != null) throw new BusinessException(ErrorCode.EXISTED_PHONE_NUMBER);
        queryWrapper.clear();

        String codedPwd = encodePwd(userAccount.getPassword());
        userAccount.setPassword(codedPwd);
        int row = userAccountMapper.insert(userAccount);

        if (row != 1) {
            throw new BusinessException(ErrorCode.REGISTRATION_FAILED);
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userAccount.getId());
        userInfoMapper.insert(userInfo);
        UserAndRole userAndRole = new UserAndRole();
        userAndRole.setUserId(userAccount.getId());
        userAndRole.setRoleId(3); //普通用户
        userRoleMapper.insert(userAndRole);
    }

    @Override
    public UserAccount logIn(String username, String password) {
        //密码加密
        String codedPwd = encodePwd(password);
        //在service 层做业务校验
        UserAccount userAccount = userAccountMapper.selectUserByUsernameAndPassword(username, codedPwd);
        if (userAccount == null) {
            throw new BusinessException(ErrorCode.INVALID_ACCOUNT);
        }
        return userAccount;
    }

    //用不上
   /* @Override
    public UserAccount checkUserAccountExistence(UserAccount userAccount) {
        QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(userAccount.getUsername()),"username", userAccount.getUsername()).or()
                .eq(StringUtils.hasText(userAccount.getEmail()),"email",userAccount.getEmail()).or()
                .eq(StringUtils.hasText(userAccount.getPhoneNumber()),"phone_number",userAccount.getPhoneNumber());
        UserAccount existedAccount = userAccountMapper.selectOne(queryWrapper, true);
        if (existedAccount != null) {

        }
    }*/

    private static String encodePwd(String password) {
        String codedPwd = DigestUtils.md5DigestAsHex(password.getBytes());
        return codedPwd;
    }
}
